﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace OgrIsleri.DashBoard.Modul.Dersler
{
    public partial class Liste : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            DonemDoldur();
           

            if (Request.QueryString["Guncelle"] != null)
                Guncelle();

            if (Request.QueryString["Sil"] != null)
                Sil();

            VeriDoldur();
        }
        private void VeriDoldur()
        {
            using (DbEntities db = new DbEntities())
            {
                var data = db.tblDerslers.OrderBy(o => o.DonemID).ToList();
                RptListe.DataSource = data;
                RptListe.DataBind();
            }
        }
        private void Sil()
        {
            using (DbEntities db = new DbEntities())
            {
                int DersID = Convert.ToInt32(Request.QueryString["Sil"]);
                var data = db.tblDerslers.Where(o => o.DersID == DersID).SingleOrDefault();

                db.tblDerslers.Remove(data);
                db.SaveChanges();
            }
            Response.Redirect("Liste.aspx");
        }
        private void Guncelle()
        {
            using (DbEntities db = new DbEntities())
            {
                int DersID=Convert.ToInt32(Request.QueryString["Guncelle"]);
                var data = db.tblDerslers.Where(o => o.DersID == DersID).SingleOrDefault();

                DpDonemler.ClearSelection();
                DpDonemler.Items.FindByValue(data.DonemID.ToString()).Selected = true;
                txtAd.Text = data.DersAdi;

                BtnKaydet.Text = "Düzenle";
            }
        }
        private void DonemDoldur()
        {
            using (DbEntities db = new DbEntities())
            {
                var data = db.tblDonemlers.OrderBy(o => o.DonemAdi).ToList();
                DpDonemler.DataSource = data;
                DpDonemler.DataTextField = "DonemAdi";
                DpDonemler.DataValueField = "DonemID";
                DpDonemler.DataBind();
            }
        }

        protected void BtnKaydet_Click(object sender, EventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
                if (Request.QueryString["Guncelle"] != null)
                {
                    int DersID = Convert.ToInt32(Request.QueryString["Guncelle"]);
                    var data = db.tblDerslers.Where(o => o.DersID == DersID).SingleOrDefault();
                    data.DonemID = Convert.ToInt32(DpDonemler.SelectedValue);
                    data.DersAdi = txtAd.Text;
                    db.SaveChanges();
                }
                else
                {
                    var data = new tblDersler();
                    data.DonemID = Convert.ToInt32(DpDonemler.SelectedValue);
                    data.DersAdi = txtAd.Text;
                    db.tblDerslers.Add(data);
                    db.SaveChanges();
                }
            }
            Response.Redirect("Liste.aspx");
        }
    }
}